<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'core/app/AppAjax.php';
require_once 'core/common/CommonEntryView.php';


class BulkFileEntryView_two extends AppView
{
	
	var $template = 'form_two.html';
	
	function execute(&$obj, &$manager, $files) {
		
		$this->addMsg('user_msg.ini');
		
		$tpl = &new tplTemplatez($this->template_dir . $this->template);
		$tpl->tplAssign('error_msg', AppMsg::errorBox($obj->errors, $this->controller->module));
		
		// categories
		FileEntryView_common::parseCategories($obj, $manager, $tpl);		
		
		// member roles
		if($manager->isExtra('private')) {
			require_once APP_EXTRA_MODULE_DIR . 'private/PrivateFile.php';
			PrivateFile::getFormStaff($tpl, $manager);
		}
		
		//files ajax
		ini_set('arg_separator.output', '&amp;'); // required for IIS
		
		$this->manager = &$manager;
		$xajax = &new AppAjax();
		//$xajax->debugOn();
		$xajax->setRequestURI($this->controller->getLink('all'));
		$xajax->registerFunction(array('getFileList', $this, 'ajaxGetFileList'));
		$xajax_js = $xajax->getJavascript();
		$xajax->processRequests();

		$tpl->tplAssign('xajax_js', $xajax_js);
		
		
		$tpl->tplAssign($this->setCommonFormVars($obj));
		$tpl->tplAssign($this->setStatusFormVars($obj->get('active')));
		$tpl->tplAssign($obj->get());
		$tpl->tplAssign($this->msg);
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function getFileList($files) {
		
		$tpl = &new tplTemplatez($this->template_dir . 'file_list.html');
		
		foreach($files as $v) {
			$a['filename'] = $v;
			$a['size'] = filesize($v);
			$tpl->tplParse($a, 'row');
		}
		
		$tpl->tplParse($this->msg);
		return $tpl->tplPrint(1);		
	}
	
	
	function ajaxGetFileList($directory) {
		
		$files = $this->manager->readDirectory($directory);
		
		if(APP_DEMO_MODE) {
			$msgs = AppMsg::getMsgs('after_action_msg.ini', false, 'not_allowed_demo');
			$files = BoxMsg::factory('error', $msgs);
		
		} elseif($files) {
			$files = $this->getFileList($files);
		
		} else {
			
			$msgs = AppMsg::getMsgs('error_msg.ini', $this->controller->module);
			if(!is_readable($directory)) {
				$msg['body'] = $msgs['dir_not_readable_msg'];
			} else {
				$msg['body'] = $msgs['no_files_in_directory'];
			}

			$files = BoxMsg::factory('error', $msg);
		}
		
		
		$objResponse = new xajaxResponse();
		$objResponse->setCharEncoding($this->encoding);
		
		//$objResponse->addAlert($directory);
		$objResponse->addAssign("file_root", "innerHTML", $files);
	
		return $objResponse;	
	}
}
?>